package com.padakeji.android.ui.widget;

import java.util.HashSet;
import java.util.Set;

import android.widget.BaseExpandableListAdapter;

public abstract class PinnedHeaderListAdapter extends BaseExpandableListAdapter {
	
	private Set<Integer> expandedGroupPositions = new HashSet<Integer>();
	
	/**
	 * {@inheritDoc}
	 * 
	 * Instead of overriding this method, you should override {@link #whenGroupCollapsed(int)} 
	 * 
	 */
	@Override
	public final void onGroupCollapsed(int groupPosition) {
		super.onGroupCollapsed(groupPosition);
		if (!expandedGroupPositions.remove(groupPosition)) {
			throw new RuntimeException();
		}
		whenGroupCollapsed(groupPosition);
	}
	
	/**
	 * {@inheritDoc}
	 * 
	 * Instead of overriding this method, you should override {@link #whenGroupExpanded(int)}
	 * 
	 */
	
	@Override
	public final void onGroupExpanded(int groupPosition) {
		super.onGroupExpanded(groupPosition);
		if (!expandedGroupPositions.add(groupPosition)) {
			throw new RuntimeException();
		}
		whenGroupExpanded(groupPosition);
	}
	
	/**
	 * Called by {@link #onGroupCollapsed(int)}
	 * Override this method to perform some work when a group is collapsed.
	 *  
	 * @param groupPosition
	 */
	public void whenGroupCollapsed(int groupPosition) {
		
	}
	
	/**
	 * Called by {@link #onGroupExpanded(int)}
	 * Override this method to perform some work when a group is collapsed.
	 * 
	 * @param groupPosition
	 */
	public void whenGroupExpanded(int groupPosition) {
		
	}
	

	public final boolean isGroupExpanded(int groupPosition) {
		return expandedGroupPositions.contains(groupPosition);
	}
	
}
